import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CoreAddonDownloadService extends BaseService<any> {
  private readonly logger = new Logger(CoreAddonDownloadService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * download
   * 对应 PHP: CoreAddonDownloadService_core::download()
   * 逻辑类型: undefined - undefined
   */
  async download(app_key: any, version: any) {
    // 基于PHP真实逻辑: download
    // PHP原文: if (!extension_loaded('zip')) throw new AddonException('ZIP_ARCHIVE_NOT_INSTALL');         $app_path = $this->addon_path . $app_key . DIRECTORY_SEPARA...
if (!extension_loaded('zip')) throw new AddonBusinessException('ZIP_ARCHIVE_NOT_INSTALL');
        const app_path = this.addon_path . app_key . DIRECTORY_SEPARATOR;
        //先判断当前的应用在本地是否存在
//        if(is_dir(app_path)) throw this.niucloudBusinessExceptionService;
        //下载文件到本地
        const zip_file = this.coreAddonCloudService.downloadAddon(app_key, version];
        //解压到应用addon下
        //删除旧版本文件
        del_target_dir(app_path, true];
        //解压文件
        this.unzip(zip_file, this.addon_path);
        //删除压缩包
        @del_target_dir(dirname(zip_file), true];
        return true;
    }

    /**
     * 解压压缩包
     * @param file
     * @param dir
     * @return mixed|string
     */
    async unzip(file, dir)
    {
        if (!file_exists(file)) throw new AddonBusinessException('ZIP_FILE_NOT_FOUND');
        const zip = this.zipArchiveService;
        if (zip.open(file) === TRUE) {
            // 对Zip文件进行解压缩操作
            zip.extractTo(dir];
            zip.close();
  }

  /**
   * unzip
   * 对应 PHP: CoreAddonDownloadService_core::unzip()
   * 逻辑类型: undefined - undefined
   */
  async unzip(file: any, dir: any) {
    // 基于PHP真实逻辑: unzip
    // PHP原文: if (!file_exists($file)) throw new AddonException('ZIP_FILE_NOT_FOUND');         $zip = new ZipArchive();         if ($zip->open($file) === TRUE) {   ...
if (!file_exists(file)) throw new AddonBusinessException('ZIP_FILE_NOT_FOUND');
        const zip = this.zipArchiveService;
        if (zip.open(file) === TRUE) {
            // 对Zip文件进行解压缩操作
            zip.extractTo(dir];
            zip.close();
  }
}
